package com.sicilin.system.role.action;

import com.sicilin.common.ajax.vo.AjaxResponse;
import com.sicilin.common.exception.BusinessException;
import com.sicilin.common.json.easyui.vo.DatagridResponse;
import com.sicilin.common.page.PageObject;
import com.sicilin.common.page.PageUtils;
import com.sicilin.po.TSysRole;
import com.sicilin.po.TSysRoleAuth;
import com.sicilin.system.login.dto.LoginUser;
import com.sicilin.system.login.service.LoginService;
import com.sicilin.system.role.dto.Role;
import com.sicilin.system.role.service.RoleService;
import com.sicilin.system.user.Admin;
import com.sicilin.system.user.util.UserUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

@Controller
@RequestMapping("/roleAction")
public class RoleAction {

    @Autowired
    private RoleService roleService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private Admin admin;

    @RequestMapping(value = "/searchRoleLimit.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("system:role:search")
    public DatagridResponse<Role> searchRoleLimit(HttpServletRequest request, @RequestParam("text") String text)throws Exception{
        PageObject<Role> pageObject = PageUtils.initPageObject(request);
        int companyId = loginService.getCurrentUser().getCompanyId();
        pageObject = roleService.searchRoleLimit(pageObject,text,companyId);
        return new DatagridResponse<Role>(pageObject);
    }

    @RequestMapping(value = "/addRole.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("system:role:save")
    public AjaxResponse addRole(@RequestBody TSysRole role)throws Exception{
        LoginUser loginUser = loginService.getCurrentUser();
        //当非管理员添加角色时候只能添加当前用户所属公司的角色
        if(!admin.getAccount().equals(loginUser.getAccount())){
            role.setCompanyId(loginUser.getCompanyId());
        }
        if(role.getLevel() == null){throw new BusinessException("请先选择角色等级！");}
        if(role.getLevel().compareTo(UserUtils.getMaxRoleLevel(loginUser)) <= 0){
            throw new BusinessException("权限不足，无法添加比自己高等级的角色！");
        }
        int roleId = roleService.saveRole(role.getRoleCode(),role.getRoleName(),role.getDescription(),role.getCompanyId(),role.getLevel());
        return new AjaxResponse(roleId);
    }


    @RequestMapping(value = "/updateRole.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("system:role:update")
    public AjaxResponse updateRole(@RequestBody TSysRole role) throws Exception{
        Integer roleId = role.getRoleId();
        if(StringUtils.isBlank(role.getRoleCode())){throw new BusinessException("角色编码不能为空！");}
        if(StringUtils.isBlank(role.getRoleName())){throw new BusinessException("角色名称不能为空！");}
        if(roleId == null|| roleId == 0){throw new BusinessException("没有选择角色！");}
        if(role.getLevel() == null){throw new BusinessException("没有选择角色等级！");}
        int maxLevel = UserUtils.getMaxRoleLevel(loginService.getCurrentUser());
        return new AjaxResponse(roleService.updateRole(role.getRoleId(),role.getRoleCode(),role.getRoleName(),role.getDescription(),role.getLevel(),maxLevel));
    }

    @RequestMapping(value = "/addRoleAuth.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"system:role:saveRoleAuth"})
    public AjaxResponse addRoleAuth(@RequestBody Set<TSysRoleAuth> roleAuths) throws Exception{
        if(roleAuths == null || roleAuths.isEmpty()
                || (roleAuths.size() == 1 && roleAuths.contains(null))){
            throw new BusinessException("没有要添加的角色权限！");
        }
        int maxLevel = UserUtils.getMaxRoleLevel(loginService.getCurrentUser());
        return new AjaxResponse(roleService.saveRoleAuths(roleAuths,maxLevel));
    }

    @RequestMapping(value = "/getRoleAuthIds.json",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("system:role:search")
    public AjaxResponse getRoleAuthIds(Integer roleId)throws Exception{
        if(roleId == null || roleId == 0){throw new BusinessException("没有选择角色！");}
        Set<Integer> authIds=roleService.getRoleAuthIdsByRoleId(roleId);
        if(authIds == null || authIds.isEmpty()
                || (authIds.size() == 1 && authIds.contains(null))){
            authIds = new HashSet<Integer>();
            authIds.add(0);
        }
        return new AjaxResponse(authIds);
    }
}
